package Beans;

import BO.Service;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import UI.*;
import java.io.Serializable;

public class ChatBean implements Serializable{
    private int chatId;
    private BO.Service service = new Service();
    private String message;
    
    
    
    
    /** Creates a new instance of ChatBean */
    public ChatBean() {
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public UI.ChatUI getChat() {
        return service.getChatByID(chatId);
    }

    public int getChatId() {
        return chatId;
    }

    public void setChatId(int chatId) {
        this.chatId = chatId;
    }
    
    public void postMessage(){
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        UI.UserUI user = ( UI.UserUI )session.getAttribute("User");
        
        service.createChatMessage(1, message, user.getId() );
        
        message = "";
    }
    
    public List<ChatMessageUI> getMessages(){
        return Service.getChatMessages();
    }
    
    
    
    
}
